     package com.delaphone.fadmin.action;

import com.delaphone.fadmin.model.FAdmins;
import com.delaphone.fadmin.service.AdminService;
import com.delaphone.flocash.util.SecurityUtil;
import com.delaphone.flocash.web.util.Validate;
import com.opensymphony.xwork2.Action;
import java.util.Date;

/**
 * Login Action Controller
 * @author dongtv
 */
public class AddUserAction extends CoreAction {

    private String username;
    private String password;
    private String firstname;
    private String lastname;
    private String email;
    private byte roleId;
    private String message;
    private AdminService adminService;

    @Override
    public String execute() throws Exception {
        //Step1: Validate
        if (Validate.isEmptyString(username)) {
            addFieldError("username", "*");
        }
        if (Validate.isEmptyString(password)) {
            addFieldError("password", "*");
        }

        if (!Validate.isEmail(email)) {
            addFieldError("email", "invalid email");
        }

        if (!getFieldErrors().isEmpty()) {
            addActionError("invalid information!");
        }
        if (!getActionErrors().isEmpty()) {
            return Action.INPUT;
        }
//        logger.info("Email:" + email);
//
//        //Step2: check username & password (will add later)
//
        FAdmins adminCreate = new FAdmins(username, SecurityUtil.makePwd(password), email, "yes", roleId, new Date());
        adminCreate.setSuspended("no");
        adminCreate.setFirstName(firstname);
        adminCreate.setLastName(lastname);
        if (adminService.createAdminUser(adminCreate)) {
            message = "User " + username + " create succesfully! ";
            return SUCCESS;
        } else {
            addActionError(getText("user.add.fail"));
            return INPUT;
        }

    }
    
    // <editor-fold defaultstate="collapsed" desc="get and set methods">

    public AdminService getAdminService() {
        return adminService;
    }

    public void setAdminService(AdminService adminService) {
        this.adminService = adminService;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public byte getRoleId() {
        return roleId;
    }

    public void setRoleId(byte roleId) {
        this.roleId = roleId;
    }

  

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    // </editor-fold>
}
